﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using KBS8.Presentation.MapView.DrawHandler;
using KBS8.Shared;

namespace KBS8.Presentation.MapView
{
    internal class MapView
    {
        /// <summary>
        /// The inner canvas that is movable
        /// </summary>
        private readonly Canvas _canvas;
        /// <summary>
        /// The tiles that are rendered
        /// </summary>
        private List<MapTile> _tileList;
        /// <summary>
        /// The first handler in the chain of responsibility to draw the map
        /// </summary>
        private DrawHandler.DrawHandler _drawHandler;

        /// <summary>
        /// Creates a new instance of MapView that draws the map on the specified canvas
        /// </summary>
        /// <param name="c">The canvas </param>
        public MapView(Canvas c)
        {
            _canvas = c;
            _tileList = new List<MapTile>();
            CreateChain(false);

            //Dummy maptiles
            MapTile t1 = new MapTile(0, 0, 2, 0, Colors.Black);
            MapTile t2 = new MapTile(0, 1, 2, 0, Colors.Black);
            MapTile t3 = new MapTile(0, 2, 2, 0, Colors.Black);
            MapTile t4 = new MapTile(1, 0, 2, 0, Colors.Black);
            MapTile t5 = new MapTile(1, 1, 2, 0, Colors.Black);
            MapTile t6 = new MapTile(1, 2, 2, 0, Colors.Black);
            MapTile t7 = new MapTile(2, 0, 2, 0, Colors.Black);
            MapTile t8 = new MapTile(2, 1, 2, 0, Colors.Black);
            MapTile t9 = new MapTile(2, 2, 2, 0, Colors.Black);
            _tileList.Add(t1);
            _tileList.Add(t2);
            _tileList.Add(t3);
            _tileList.Add(t4);
            _tileList.Add(t5);
            _tileList.Add(t6);
            _tileList.Add(t7);
            _tileList.Add(t8);
            _tileList.Add(t9);
            //end dummy

            //Handle();
        }
        public void CreateChain(bool poisEnabled)
        {
            GrassDrawHandler gdh = new GrassDrawHandler();
            WaterDrawHandler wdh = new WaterDrawHandler();
            IndustrialDrawHandler idh = new IndustrialDrawHandler();
            ForestDrawHandler fdh = new ForestDrawHandler();
            CroplandDrawHandler cldh = new CroplandDrawHandler();
            CityDrawHandler cdh = new CityDrawHandler();
            BeachDrawHandler bdh = new BeachDrawHandler();
            LandroadDrawHandler ldh = new LandroadDrawHandler();
            CityroadDrawHandler crdh = new CityroadDrawHandler();
            CountyroadDrawHandler codh = new CountyroadDrawHandler();
            RailroadDrawHandler rdh = new RailroadDrawHandler();
            HighwayDrawHandler hdh = new HighwayDrawHandler();

            _drawHandler = gdh;
            gdh.SetSuccessor(wdh);
            wdh.SetSuccessor(idh);
            idh.SetSuccessor(fdh);
            fdh.SetSuccessor(cldh);
            cldh.SetSuccessor(cdh);
            cdh.SetSuccessor(bdh);
            bdh.SetSuccessor(ldh);
            ldh.SetSuccessor(crdh);
            crdh.SetSuccessor(codh);
            codh.SetSuccessor(rdh);
            rdh.SetSuccessor(hdh);
        }
        public void Handle()
        {
            foreach (MapTile tile in _tileList)
            {
                DrawingVisual drawingVisual = new DrawingVisual();
                DrawingContext drawingContext = drawingVisual.RenderOpen();
                _drawHandler.Handle(tile, ref drawingContext);
                drawingContext.Close();
                VisualMapTile vmt = new VisualMapTile(drawingVisual) {Visibility = Visibility.Visible};
                this._canvas.Children.Add(vmt);
                Canvas.SetLeft(vmt, tile.X * tile.rec.Width);
                Canvas.SetTop(vmt, tile.Y * tile.rec.Height);
            }
        }
    }
}
